import scrapy
from scrapy_04_dytt.items import Scrapy04DyttItem

class DyttSpider(scrapy.Spider):
    name = "dytt"
    allowed_domains = ["www.dygod.net"]
    start_urls = ["https://www.dygod.net/html/tv/hytv/index.html"]
    page=1
    base_url="https://www.dygod.net/html/tv/hytv/"
    def parse(self, response):
        # 要第一页的名字和第二页的图片 name pic_url
        item_list=response.xpath('//div[@class="co_content8"]//ul//table')

        for item in item_list:
            name_list=item.xpath('./tr[2]//td[2]//a/text()')
            name=name_list.extract_first()
            href_list = item.xpath('./tr[2]//td[2]//a/@href')
            href = "https://www.dygod.net"+href_list.extract_first()
            print(name,href)
            # 向跳转页获取数据
            yield scrapy.Request(url=href,callback=self.parse_second,meta={"name":name})
        # 获取下一页地址
        if self.page<20:
            print("=====================第"+str(self.page)+"数据结束！")
            self.page=self.page+1
            nextPage_url=self.base_url+'index_'+str(self.page)+'.html'
            yield scrapy.Request(url=nextPage_url,callback=self.parse)


    def parse_second(self,response):
        image_list=response.xpath('//div[@id="Zoom"]//img/@src')
        image_url='https://www.dygod.net'+image_list.extract_first()
        name=response.meta['name']
        movie=Scrapy04DyttItem(name=name,image_url=image_url)
        yield movie